/**
 * Created by lenovo on 14-12-26.
 */

$(document).ready(function(){

    //选项卡
    $(".coat_nav ul li").click(function(){
        $(".coat_nav ul li").removeClass("active");
        $(this).addClass("active")
        var type=$(this).attr("id");
        loadGoods(type);
    });

    //侧边栏
    $(".sidebar ul li:first-child").click( function(){
    	viewCart();
        if($(".sidebar").css("right")== "-300px" ){
            $(".sidebar").animate({"right":"0"},500);
        }else{
            $(".sidebar").animate({"right":"-300px"},500);
        }
    });
    
    //详情
    $(".det_box a").hover(function(){
        $(this).parent().find(".detail").toggle();
    });
    
    loadGoods();
    viewCart();
});


function loadGoods(type){
	if(!type){
		type=0;
	}
	$.ajax({
		type : "GET",
		dataType : "json",
		url : '/iwash/goods/goods/listGoods.do',
		data : {"type":type},
		success : function(data) {
			var html='';
			if(data.length>0){
				html = template("index-gl", {list : data});  
			}else{
				html='更多服务，敬请期待！';
			}
			$('.kind1').html(html);
		}
	});
}

function viewCart(){
	$.ajax({
		type : "GET",
		dataType : "json",
		url : '/iwash/cart/shoppingCart/viewMyCart.do',
		success : function(data) {
			$('#cartCount').html(data.count);
			var html='';
			if(data && data.cartItems.length>0){
			    html = template("cart", {cart:data});
			}else{
				html='来都来了，不洗几件衣服嘛';
			}
			$('.cart-list').html(html);
		}
	});
}

function addGnum(obj,flag){
	var num = parseInt($(obj).parent().find(":text").val());
	if (num.toString() == "NaN") {
		num=1;
	}
	if(flag==1){
		num++;
	}else if(flag==-1){
		if(num>1){
			num--;
		}
	}
	$(obj).parent().find(":text").val(num);
}

function delGoods(goodsIds){
	
	$.ajax({
		type : "POST",
		url : '/iwash/cart/shoppingCart/removeGoodsInCart.do',
		data : {"goodsIds":goodsIds},
		success : function(result) {
			viewCart();
		}
	});
}

function addToCart(obj,goodsId){
	if($(".sidebar").css("right")!= "-300px" ){
        $(".sidebar").animate({"right":"-300px"},500);
    }
	var quantity = parseInt($(obj).parent().find(":text").val());
	$.ajax({
		type : "POST",
		url : '/iwash/cart/shoppingCart/changeGnumInCart.do',
		data : {"goodsId":goodsId,"quantity":quantity,"flag":1},
		success : function(result) {
			if(result=='0'){
				quantity=quantity+parseInt($('#cartCount').html());
				$('#cartCount').html(quantity);
			}else{
				alert(result.split('-')[1]);
			}
		}
	});
}
